#include "Menu.h"

vector<Menu*> Menu::menuStack;

Menu* Menu::getTopMenu() {
	if(menuStack.empty()) return NULL;
	else return menuStack.back();
}

void Menu::pushMenuStack(Menu* menu) {
	menuStack.push_back(menu);
}

void Menu::popMenuStack() {
	delete menuStack.back();
	menuStack.pop_back();
}

void Menu::clearMenuStack() {
	while(!menuStack.empty()) {
		delete menuStack.back();
		menuStack.pop_back();
	}
}

void Menu::drawMenuStack() {
	for(vector<Menu*>::iterator menuIter = Menu::menuStack.begin(); menuIter < Menu::menuStack.end(); menuIter++) {
		(*menuIter)->draw();
	}
}

Menu::Menu(int x, int y, vector<MenuItem> items, Scene* handler, SelectFunction selectFunction, int numColumns, bool cancelable) :
		Window(x, y, Menu::ITEM_WIDTH * numColumns,
				((items.size() + numColumns - 1) / numColumns) * Menu::ITEM_HEIGHT + Window::GRID_SIZE * 2),
		items(items), handler(handler), selectFunction(selectFunction), numColumns(numColumns), canExit(canExit), selectedIndex(0) {

}

Menu::~Menu() {

}

int Menu::getSelectedIndex() const {
	return selectedIndex;
}

void Menu::setSelectedIndex(int newIndex) {
	if(newIndex < 0 || newIndex > (int)items.size())
		selectedIndex = -1;
	else
		selectedIndex = newIndex;
}

void Menu::setItemEnabled(int index, bool newEnabled) {
	items[index].setEnabled(newEnabled);
}

void Menu::handleEvent(SDL_Event* event) {
	switch(event->type) {
	case SDL_KEYDOWN:
		switch(event->key.keysym.sym) {
		case SDLK_UP:
			if(selectedIndex < numColumns) {
				selectedIndex += ((int)items.size() / numColumns) * numColumns;
				if(selectedIndex >= (int)items.size()) selectedIndex -= numColumns;
			} else selectedIndex -= numColumns;
			break;
		case SDLK_DOWN:
			if(selectedIndex >= (int)items.size() - numColumns) {
				selectedIndex %= numColumns;
			} else selectedIndex += numColumns;
			break;
		case SDLK_LEFT:
			if(selectedIndex % numColumns) selectedIndex -= 1;
			else {
				selectedIndex += numColumns - 1;
				if(selectedIndex >= (int)items.size()) selectedIndex = (int)items.size() - 1;
			}
			break;
		case SDLK_RIGHT:
			if(selectedIndex == (int)items.size() - 1) selectedIndex = (selectedIndex / numColumns) * numColumns;
			else if((selectedIndex + 1) % numColumns) selectedIndex += 1;
			else selectedIndex -= numColumns - 1;
			break;
		case SDLK_SPACE:
		case SDLK_RETURN:
		case SDLK_KP_ENTER:
			if(selectedIndex != -1 && items[selectedIndex].isEnabled()) {
				(handler->*selectFunction)(selectedIndex);
			}
			break;
		case SDLK_ESCAPE:
			if(Menu::getTopMenu() == this && canExit) {
				Menu::popMenuStack();
			}
			break;
		default:
			break;
		}
		break;
	}
}

void Menu::drawContent() {
	int index = 0;
	Font::fontSize = 12;
	for(vector<MenuItem>::iterator itemIter = items.begin(); itemIter < items.end(); itemIter++, index++) {
		if(selectedIndex == index) {
			glColor3ub(0, 0, 0);
		} else {
			glColor3ub(128, 128, 128);
		}
		drawText(Menu::ITEM_WIDTH * (index % numColumns) + Window::GRID_SIZE,
				Menu::ITEM_HEIGHT * (index / numColumns) + Window::GRID_SIZE,
				itemIter->getText().c_str());
	}
}
